import { createRouter, createWebHashHistory } from "vue-router";
import { showToast } from "vant";
import homeView from '@/views/index.vue'
const router = createRouter({
    history: createWebHashHistory(),
    routes: [{
        path: "/",
        component: homeView,
        redirect: "/home",
		name:"Index",
		meta:{
			cache:true
		},
        children: [{
            path: "home",
			name:"home",
            component: () => import('@/views/home/index.vue'),
			meta:{
				cache:true,
				top:0
			},
			// beforeEnter(to,from){
				
			// 	console.log(to)
			// 	return true
			// }

        }, {
            path: "video",
            component: () => import('@/views/video.vue'),
			meta:{
				cache:true
			},
        }, {
            path: "my",
            component: () => import('@/views/user/index.vue'),
            meta: {
                auth: 'login',
                msg: '权限不足,请先登陆',
                redirect: '/login',
				cache:true
            }
        }]
    },
    {
        path: "/login",
        component: () => import('@/views/user/login/index.vue'),
    }, {
        path: "/reg",
        component: () => import('@/views/user/reg/index.vue'),
    }, {
        path: "/set",
        component: () => import('@/views/user/set/index.vue'),
		meta:{
			title:"设置",
			auth: 'login',
			msg: '权限不足,请先登陆',
			redirect: '/login'
		},
    },  {
        path: "/userInfo",
        component: () => import('@/views/user/userInfo/index.vue'),
		meta:{
			title:"用户信息",
			auth: 'login',
			msg: '权限不足,请先登陆',
			redirect: '/login',
			cache:true
		},
    },   {
        path: "/country",
        component: () => import('@/views/user/userInfo/country.vue'),
		
    }, 
	{
        path: "/publish",
		meta:{
			title:"发布",
			cache:true//  true 缓存
		},
        component: () => import('@/views/publish/index.vue'),
    },
	{
	    path: "/article",
		
	    component: () => import('@/views/article/index.vue'),
	},{
	    path: "/search",
		
	    component: () => import('@/views/search/index.vue'),
	},{
	    path: "/searchList",
		
	    component: () => import('@/views/search/searchList.vue'),
	}],
	scrollBehavior(to, from, savedPosition) {
		console.log(savedPosition)
		if(to.path=='/home'&&!savedPosition){
			let top = sessionStorage.getItem('home_top')||0;
				savedPosition = {
					top
				}
		}
	      
		
	      if (savedPosition) {
	          return savedPosition
	        } else {
	          return { top: 0 }
	        }
	  },
})

// 1.  设定重要页面名单

// let pageList = ['/my']

// 2.  利用路由的meta


router.beforeEach(function (to, from, next) {
    // from   从什么页面来的  资讯页面
    // to  去哪去   我的 
    // next  下一步的动作
    // console.log(from)
    // console.log(to)

    if (to.meta.auth === 'login') {
        let isLogin = !!localStorage.getItem('token')

        if (isLogin) {
            next()
        } else {

            showToast(to.meta.msg)
            setTimeout(() => {
                router.push(to.meta.redirect)
            }, 300);
        }

    } else {
        // 不是敏感页面   直接放行
        next()

    }

    // 路由切换时 
    // 检查你要去的页面是不是需要检查权限的页面
    // 如果不是  直接放行
    // 如果是，判断是否符合条件 
    //  如果有 -  放行
    //  如果没有 - 提示，重定向


})



export default router